//文件夹adapter
public class ImageFoldersAdapter extends RecyclerView.Adapter<ImageFoldersAdapter.ViewHolder> {
    
    private Context mContext;
    private List<ImageFolder> mImageFolderList;
    private int mCurrentImageFolderIndex;
    private OnImageFolderChangeListener mImageFolderChangeListener;

    public ImageFoldersAdapter(Context context, List<ImageFolder> imageFolderList, int position) {
        this.mContext = context;
        this.mImageFolderList = imageFolderList;
        this.mCurrentImageFolderIndex = position;
    }

    @NonNull
    @Override 
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recyclerview_folder, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final ImageFolder imageFolder = mImageFolderList.get(position);
        String folderCover = imageFolder.getFolderCover();
        String folderName = imageFolder.getFolderName();
        int imageSize = imageFolder.getImageObjectList().size();
        holder.mFolderName.setText(!TextUtils.isEmpty(folderName)?folderName:" ");
        holder.mImageSize.setText(String.format(mContext.getString(R.string.image_num), imageSize));
        holder.mImageFolderCheck.setVisibility(mCurrentImageFolderIndex == position?View.VISIBLE:View.GONE);
        //加载图片
        Glide.with(context).load(folderCover).into(holder.mImageCover);
        //点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCurrentImageFolderIndex = position;
                    notifyDataSetChanged();
                    mImageFolderChangeListener.onImageFolderChange(view, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mImageFolderList == null ? 0 : mImageFolderList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView mImageCover;
        private TextView mFolderName;
        private TextView mImageSize;
        private ImageView mImageFolderCheck;

        public ViewHolder(View itemView) {
            super(itemView);
            mImageCover = itemView.findViewById(R.id.iv_item_imageCover);
            mFolderName = itemView.findViewById(R.id.tv_item_folderName);
            mImageSize = itemView.findViewById(R.id.tv_item_imageSize);
            mImageFolderCheck = itemView.findViewById(R.id.iv_item_check);
        }
    }

    //点击事件回调给外部
    public void setOnImageFolderChangeListener(OnImageFolderChangeListener onItemClickListener) {
        this.mImageFolderChangeListener = onItemClickListener;
    }
    public interface OnImageFolderChangeListener {
        void onImageFolderChange(View view, int position);
    }
}
